#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128;
const int N = 1e6 + 5;

priority_queue<int, vector<int>, greater<int>> min_pq;
int a, b, ans = 0;

void solve() {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        min_pq.push(a);
    }
    while (min_pq.size() != 1) {
        a = min_pq.top();
        min_pq.pop();
        b = min_pq.top();
        min_pq.pop();
        ans += a + b;
        min_pq.push(a + b);
    }
    cout << ans << endl;
    return;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    solve();

    return 0;
}